<template>
  <div class="box">
    <Loann
      ref="loann"
      :form-data="loanInfo"
    />
    <Assure
      ref="assure"
      :form-data="guarantee"
    />
    <Datum
      ref="datum"
      :form-data="loanInformation"
    />
    
    <div class="across"></div>
    <div class="keystroke">
      <el-row>
        <el-button type="primary" @click="handleSubmit">提交审核</el-button>
        <el-button type="success" @click="handleSave">提交保存</el-button>
      </el-row>
    </div>
  </div>
</template>

<script>
 import Loann from "../../components/loan/Loann.vue"
 import Assure from "../../components/loan/Assure.vue"
 import Datum from "../../components/loan/Datum.vue"

 import {getLoanById, saveLoanInfo, submitLoanInfo, updateLoanInfo} from "../../http/loan";

 export default {
  name: "Loan0",
  data() {
    return {
      // loanInfo: {
      //   apr: 0.12, //年利率
      //   asd: "工资", //还款方式
      //   borrowCheck: null, //标的审核信息
      //   borrowSubjectInfo: null, //标的具体信息
      //   interestWay: "成立计息",
      //   loanMgrInterestRate: 0.03, //借款管理月利率
      //   loanPeriod: 5, //借款期限 （天）
      //   loanType: "续贷", //借款类型
      //   name: "新标1号", //标名
      //   overdue: 0.05, //逾期罚息利率
      //   repayment: "一次性还款", //还款方式
      //   riskLevel: "较低", //风险等级
      //   termType: "天", //期限类型（天）
      //   total: 20000, //借款金额
      //   useOfFunds: "资金周转",//资金用途
      //   userPhone: 13888888888 //借款人联系方式
      // },
      // guarantee: {
      //   guaranteed: true,
      //   mortgageType: "2",
      //   guaranteeAgency: "上海泽润典当公司",
      //   collateral: "https://internetfinance.oss-cn-chengdu.aliyuncs.com/internetfinance/2021-06-11/9b67df412a1a4c839ca463ed14a255e8.jfif,https://internetfinance.oss-cn-chengdu.aliyuncs.com/internetfinance/2021-06-11/fd5919af4a3943d68b4e87e7f0f7452b.jfif"
      // },
      // loanInformation: {
      //   loanInformation: "https://internetfinance.oss-cn-chengdu.aliyuncs.com/internetfinance/2021-06-11/9b67df412a1a4c839ca463ed14a255e8.jfif,https://internetfinance.oss-cn-chengdu.aliyuncs.com/internetfinance/2021-06-11/fd5919af4a3943d68b4e87e7f0f7452b.jfif"
      // }
      loanInfo: {},
      guarantee: {},
      loanInformation: {}
    }
  },
  components:{
    Loann,
    Assure,
    Datum
  },
  methods:{
    handleSubmit(){
      // let a = {
      //   "apr": 0.12, //年利率
      //   "asd": "工资", //还款方式
      //   "borrowCheck": null, //标的审核信息
      //   "borrowSubjectInfo": null, //标的具体信息
      //   "guarantee": { //担保对象
      //     "collateral": "抵押物材料相关图片url……", //抵押物url，“,”分割
      //     "guaranteeAgency": "上海天图典当行", //担保机构
      //     "guaranteed": false, //是否担保
      //     "mortgageType": "0,1,2,3" //抵押类型
      //   },
      //   "interestWay": "成立计息",
      //   "loanInformation": "上传的资料图片url……", //借款材料url
      //   "loanMgrInterestRate": 0.03, //借款管理月利率
      //   "loanPeriod": 5, //借款期限 （天）
      //   "loanType": "续贷", //借款类型
      //   "name": "新标1号", //标名
      //   "overdue": 0.05, //逾期罚息利率
      //   "repayment": "一次性还款", //还款方式
      //   "riskLevel": "较低", //风险等级
      //   "termType": "天", //期限类型（天）
      //   "total": 20000, //借款金额
      //   "useOfFunds": "资金周转",//资金用途
      //   "userPhone": 13888888888 //借款人联系方式
      // }

    this.uploadInfo(submitLoanInfo).then(res => {
      if(res.data.code == 20000) {
        this.$message.success("提交成功");
        this.$router.push("/loan/loanreview");
      }else {
        this.$message.error("提交失败！");
      }}).catch(err => {
          console.err(err);
          this.$message.error("提交失败！");
        });
      
      
      // let data = this.getUploadInfo();
      // (function (that, id) {
      //   if( that._.isUndefined(id)) return submitLoanInfo;
      //   else {
      //     data.id = id;
      //     return updateLoanInfo;
      //   }
      // })(this, this.$route.params.id)(data).then(res => {
      //   if(res.data.code == 20000) {
      //     this.$message.success("提交成功");
      //   }else {
      //     this.$message.error("提交失败！");
      //   }
      // }).catch(err => {
      //   console.err(err);
      //   this.$message.error("提交失败！");
      // });
    },
    handleSave() {
      this.uploadInfo(saveLoanInfo).then(res => {
        if(res.data.code == 20000) {
          this.$message.success("保存成功");
          this.$router.push("/loan/loanreview");
        }else {
          this.$message.error("保存失败！");
        }
      }).catch(err => {
        console.err(err);
        this.$message.error("保存失败！");
      });
    },
    uploadInfo(defFun) {
      let valid = 0;
      this.$refs.loann.$refs.ruleForm.validate( result => result && valid++ )//在提交前验证数据是否正确
  
      if(!valid) return Promise.reject("表单数据填写有误");
      
      let data = {
        borrowCheck: null, //标的审核信息
        borrowSubjectInfo: null, //标的具体信息
        ...this.$refs.loann.ruleForm,
        guarantee: {
          ...this.$refs.assure.ruleForm,
        },
        ...this.$refs.datum.ruleForm
      };
      let id = this.$route.params.id;
      if(this._.isUndefined(id)) return defFun(data);
      else {
        data.id = id;
        data.guarantee.baseInfoId = id;
        return  updateLoanInfo(data);
      }
    }
  },
  created() {
    let id = this.$route.params.id;
    if(!this._.isUndefined(id)) {
      getLoanById(id).then(res => {
        console.log(" getLoanById: ",res)
        if(res.data.code == 20000) {
          this.guarantee = res.data.data.info.guarantee
          this.loanInformation = res.data.data.info;
          this.loanInfo = res.data.data.info;
        }else {
          this.$message.error("请求标的详细信息出错！")
        }
        
      }).catch(err => {
        console.err(err)
        this.$message.error("请求标的详细信息出错！")
      })
    }
  }
}
</script>

<style scoped>
    .box {
      width: 91%;
      margin: 80px 50px;
    }
    .across {
      border-top: 1px solid #000;
      margin-top: -60px;
    }
    .keystroke {
      width: 260px;
      height: 15px;
      margin-top: 30px; 
      margin-left:auto;
      margin-right:auto;
    }
</style>